﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using eAssessment.Classes.Assessments;
using Microsoft.SharePoint.Utilities;
using System.Web;
using eAssessment.lists;
using eAssessment.SPLinqPortal;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using System.Data;

namespace eAssessment.Webparts.CreateAssessment
{
    public partial class CreateAssessmentUserControl : UserControl
    {
        private CreateAssessmentInput _input;

        protected void Page_Load(object sender, EventArgs e)
        {
            
            _input = (CreateAssessmentInput)Session["CreateQuestionInput"];

            string task = Request.QueryString["task"];
            if (task != null)
            {
                switch (task)
                {
                    case "delete": 
                        string qid = Request.QueryString["qid"];
                        if (qid != null)
                        {
                            _input.deleteQuestion(int.Parse(qid));
                        }
                        break;
                }
            }

            if (_input != null)
            {
                if (!Page.IsCallback)
                {
                    veranstaltungLabel.Text = SPContext.Current.Web.Title;
                    titleTextBox.Text = _input.Titel;
                    startDatePick.SelectedDate = _input.Start;
                    endDatePick.SelectedDate = _input.End;
                    selftestCheckBox.Checked = _input.Selftest;
                    pointDeductionCheckBox.Checked = _input.PointDeduction;

                    ListAccessManager mgr = new ListAccessManager();
                    foreach (int id in _input.Questions)
                    {
                        QuestionTemplate question = mgr.GetQuestionTemplate(id);

                        TableCell themeCell = new TableCell();
                        themeCell.Text = question.Theme;

                        TableCell questionCell = new TableCell();
                        questionCell.Text = question.QuestionText;

                        TableCell typeCell = new TableCell();
                        typeCell.Text = question.Type.ToString();

                        TableCell showCell = new TableCell();
                        HyperLink showLink = new HyperLink();
                        showLink.NavigateUrl = SPContext.Current.Web.Url + "/sites/CreateAssessment.aspx?task=show&qid=" + id;
                        showLink.Text = "anzeigen";
                        showLink.Controls.Add(showLink);

                        TableCell deleteCell = new TableCell();
                        HyperLink deleteLink = new HyperLink();
                        deleteLink.NavigateUrl = SPContext.Current.Web.Url + "/sites/CreateAssessment.aspx?task=delete&qid=" + id;
                        deleteLink.Text = "löschen";
                        deleteCell.Controls.Add(deleteLink);

                        TableRow row = new TableRow();
                        row.Cells.Add(themeCell);
                        row.Cells.Add(questionCell);
                        row.Cells.Add(typeCell);
                        row.Cells.Add(showCell);
                        row.Cells.Add(deleteCell);
                        

                        questionBlockTable0.Rows.Add(row);
                    }
                }
            }
            else
            {
                _input = new CreateAssessmentInput();
            }
        }

        protected void addQuestionButton_Click(object sender, EventArgs e)
        {
            _input.Titel = titleTextBox.Text;
            _input.Start = startDatePick.SelectedDate;
            _input.End = endDatePick.SelectedDate;
            _input.Selftest = selftestCheckBox.Checked;
            _input.PointDeduction = pointDeductionCheckBox.Checked;
            Session["CreateQuestionInput"] = _input;
            SPUtility.Redirect("AddQuestionToAssessment.aspx", SPRedirectFlags.Default, HttpContext.Current);
        }

        protected void createAssessmentButton_Click(object sender, EventArgs e)
        {
            // template assessment erstellen
            // zuordnung tempfragen - temp assessment
            // assessment active erstellen 
            // assessment active zuordnung erstellen
        }

        protected void resetButton_Click(object sender, EventArgs e)
        {
            
            _input = null;
            titleTextBox.Text = "";
            startDatePick.SelectedDate = new DateTime();
            endDatePick.SelectedDate = new DateTime();
            selftestCheckBox.Checked = false;
            pointDeductionCheckBox.Checked = false;
            questionBlockTable0.Rows.Clear();
            Session["CreateQuestionInput"] = null;
        }
    }
}
